package com.example.weektwo.dailysupplies.Three;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.weektwo.Bean.ContentDatas;
import com.example.weektwo.Bean.InfoBean;
import com.example.weektwo.R;
import com.example.weektwo.dailysupplies.four.PayActivity;

import java.util.List;

public class ShoppingCartActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tv_title,price;
    ImageView iv_back;
    ListView shopLv;
    Button submit;
    private ShopAdapter adapter;
    private List<InfoBean> buyList;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//             更新数据源，重构数据
            adapter.notifyDataSetChanged();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_cart);
        tv_title=findViewById(R.id.top_tv);
        tv_title.setText("购物车");
        submit=findViewById(R.id.shopcar_submit);
        submit.setOnClickListener(this);
        price=findViewById(R.id.shop_car_price);
        iv_back=findViewById(R.id.top_iv);
        iv_back.setOnClickListener(this);
        shopLv=findViewById(R.id.shopcar_lv);
        buyList = ContentDatas.buyList;
        adapter=new ShopAdapter(this,buyList);
        shopLv.setAdapter(adapter);
        handler.sendEmptyMessageDelayed(1,2000);
        caculateTotalMoney();    //计算总钱数，显示在TextView上

    }

    //    计算购物车中购买的商品价格的方法
    public void caculateTotalMoney(){
        List<InfoBean> buyList = ContentDatas.buyList;
        double total_money = 0;
        for (int i = 0; i < buyList.size(); i++) {
            InfoBean bean = buyList.get(i);
            double price = bean.getPrice();
            int buycount = bean.getBuycount();
            total_money=price*buycount+total_money;
        }
        price.setText("￥"+total_money);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_iv:
                finish();
                break;
            case R.id.shopcar_submit:
                Intent intent = new Intent(this, PayActivity.class);
                startActivity(intent);
                break;
        }
    }
}
